/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.maincoupon.rest;

import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.maincoupon.domain.TMainCoupon;
import com.xunyin.modules.maincoupon.service.TMainCouponService;
import com.xunyin.modules.maincoupon.service.dto.TMainCouponQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-27
 */
@AllArgsConstructor
@Api(tags = "共享空间：优惠券设置管理")
@RestController
@RequestMapping("api")
public class TMainCouponController {

    private final TMainCouponService tMainCouponService;
    private final IGenerator generator;

    @Log("查询共享空间：优惠券设置")
    @ApiOperation("查询共享空间：优惠券设置")
    @GetMapping(value = "/tMainCoupon")
    @PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getTMainCoupons(TMainCouponQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> queryAll = tMainCouponService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增共享空间：优惠券设置")
    @ApiOperation("新增共享空间：优惠券设置")
    @PostMapping(value = "/tMainCoupon")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody TMainCoupon resources) {
        return new ResponseEntity<>(tMainCouponService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改共享空间：优惠券设置")
    @ApiOperation("修改共享空间：优惠券设置")
    @PutMapping(value = "/tMainCoupon")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody TMainCoupon resources) {
        tMainCouponService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除共享空间：优惠券设置")
    @ApiOperation("删除共享空间：优惠券设置")
    @DeleteMapping(value = "/tMainCoupon/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tMainCouponService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取优惠券列表")
    @ApiOperation("获取优惠券列表")
    @PostMapping(value = "/listMainCoupon")
    public ResponseEntity<Object> listMainCoupon() {
        return new ResponseEntity<>(tMainCouponService.listMainCoupon(), HttpStatus.OK);
    }
}
